<?php

namespace app\store\controller\goods;

use app\store\controller\Controller;
use app\store\model\Category as CategoryModel;
use think\Cache;
/**
 * 商品分类
 * Class Category
 * @package app\store\controller\goods
 */
class Category extends Controller
{
    public function toggleShow()
    {
        if (!$this->request->isAjax()) {
            return json(['code' => 0, 'msg' => '非法请求']);
        }
    
        // 获取请求数据
        $categoryId = input('post.category_id/d');
        $isShow = input('post.is_show/d', 0);
    
        // 查询分类
        $category = CategoryModel::get($categoryId);
        if (!$category) {
            return json(['code' => 0, 'msg' => '分类不存在']);
        }
    
        // 更新 `is_show` 状态
        $category->save(['is_show' => $isShow]);
    
        // ✅ 清除旧缓存，重新生成新缓存
        Cache::rm('category_' . $category->wxapp_id);  // 删除缓存
        // Category::getALL(); // 重新生成缓存
    
        return json(['code' => 1, 'msg' => '状态更新成功']);
    }
    public function toggleZtShow()
    {
        $categoryId = input('post.category_id');
        $isZtShow = input('post.is_zt_show');

        if (!$categoryId) {
            return $this->renderError('参数错误');
        }
        $model = CategoryModel::get($categoryId);
        if (!$model) {
            return $this->renderError('分类不存在');
        }
        if ($model->save(['is_zt_show' => $isZtShow])) {
            // ✅ 清除旧缓存，重新生成新缓存
            Cache::rm('category_' . $model->wxapp_id.'_'.'1');  // 删除缓存
            return $this->renderSuccess('设置成功');
        } else {
            return $this->renderError('设置失败');
        }
    }
    /**
     * 商品分类列表
     * @return mixed
     */
    public function index()
    {
        $model = new CategoryModel;
        // echo 1;die;
        $list = $model->getCacheTree();
        // dump($list);die;
        return $this->fetch('index', compact('list'));
    }

    /**
     * 删除商品分类
     * @param $category_id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function delete($category_id)
    {
        $model = CategoryModel::get($category_id);
        if (!$model->remove($category_id)) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 添加商品分类
     * @return array|mixed
     */
    public function add()
    {
        $model = new CategoryModel;
        if (!$this->request->isAjax()) {
            // 获取所有地区
            $list = $model->getCacheTree();
            return $this->fetch('add', compact('list'));
        }
        // 新增记录
        if ($model->add($this->postData('category'))) {
            return $this->renderSuccess('添加成功', url('goods.category/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑商品分类
     * @param $category_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit($category_id)
    {
        // 模板详情
        $model = CategoryModel::get($category_id, ['image', 'ztImage']);
        // dump($model->toArray());die;
        if (!$this->request->isAjax()) {
            // 获取所有地区
            $list = $model->getCacheTree();
            return $this->fetch('edit', compact('model', 'list'));
        }
        // 更新记录
        if ($model->edit($this->postData('category'))) {
            return $this->renderSuccess('更新成功', url('goods.category/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

}
